package com.ruoyi.demowechatpay.controller;


import com.ruoyi.demowechatpay.enumeration.TransferDetailStatusEnum;
import com.ruoyi.demowechatpay.pojo.dto.MyTransferCreateRequest;
import com.ruoyi.demowechatpay.pojo.dto.MyWxBatchesQueryRequest;
import com.ruoyi.demowechatpay.pojo.dto.WeChatTransferDTO;
import com.ruoyi.demowechatpay.pojo.dto.WeChatTransferQueryDTO;
import com.ruoyi.demowechatpay.pojo.vo.MyBatchesQueryResult;
import com.ruoyi.demowechatpay.pojo.vo.MyTransferCreateResult;
import com.ruoyi.demowechatpay.service.BusinessNumberManager;
import com.ruoyi.demowechatpay.service.MyMerchantTransferServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Objects;

/**
 * Test
 *
 * @author huajiao
 * @date 2025/3/27 10:13
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/test")
public class Test {

    private final MyMerchantTransferServiceImpl myMerchantTransferServiceImpl;

    @PostMapping("/transferBills")
    public MyTransferCreateResult transferBills(@RequestBody WeChatTransferDTO transferDTO) {

        MyTransferCreateRequest request = MyTransferCreateRequest.builder()
                .appid(transferDTO.getAppId())
                .openid(transferDTO.getOpenid())
                .userName(transferDTO.getUserName())
                .outBillNo(BusinessNumberManager.generateShortV2("BILL"))
                .transferAmount(transferDTO.getTransferAmount())
                .transferRemark(transferDTO.getBatchRemark())
                .transferSceneId("1005")
                .build();
        MyTransferCreateRequest.TransferSceneReportInfo info = new MyTransferCreateRequest.TransferSceneReportInfo();
        info.setInfoType("岗位类型");
        info.setInfoContent("修车师傅");
        MyTransferCreateRequest.TransferSceneReportInfo info1 = new MyTransferCreateRequest.TransferSceneReportInfo();
        info1.setInfoType("报酬说明");
        info1.setInfoContent("用户本月佣金");
        request.setTransferSceneReportInfos(Arrays.asList(info, info1));

        log.info("微信转账到零钱请求参数：{}", request.toString());
        try {
            MyTransferCreateResult result = myMerchantTransferServiceImpl.transferBills(request);
            return result;
        } catch (Exception e) {
            log.error("微信转账到零钱调用失败,{}", e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("微信转账到零钱调用失败");
        }
    }


    @PostMapping("/queryTransfer")
    public TransferDetailStatusEnum queryTransfer(@RequestBody WeChatTransferQueryDTO dto) {
        MyWxBatchesQueryRequest queryRequest = MyWxBatchesQueryRequest.builder().transferBillNo(dto.getTransferBillNo())
                .build();
        log.info("查询微信转账到零钱请求参数：{}", queryRequest.toString());
        try {
            MyBatchesQueryResult result = myMerchantTransferServiceImpl.queryTransferBillNo(queryRequest);
            if (Objects.nonNull(result)) {
                return TransferDetailStatusEnum.valueOf(result.getState());
            }
            return TransferDetailStatusEnum.FAIL;
        } catch (Exception e) {
            log.error("查询微信转账到零钱调用失败,{}", e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("查询微信转账到零钱调用失败");
        }
    }
}
